package com.itheima.manager;

import com.itheima.domain.UmsMenu;
import com.itheima.service.db.UmsMenuService;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UmsMenuVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName UmsMenuManager
 * @date 2022/11/12 22:20
 */
@Component
public class UmsMenuManager {

    @DubboReference
    UmsMenuService umsMenuService;

    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    public R getTreeList() {
        List<UmsMenuVo> treeList = umsMenuService.getTreeList();
        return R.success(treeList);
    }

    /**
     * 分页查询后台菜单
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public R getList(Long parentId, Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = umsMenuService.getList(parentId,pageNum,pageSize);
        return R.success(pageBeanVo);
    }

    /**
     * 添加后台菜单
     * @param umsMenu
     * @return
     */
    public R createBackgroundMenu(UmsMenu umsMenu) {
        umsMenuService.createBackgroundMenu(umsMenu);
        return R.success("添加成功");
    }

    /**
     * 根据ID获取菜单详情
     * @param id
     * @return
     */
    public R getMenuDetails(Long id) {
        UmsMenu umsMenu = umsMenuService.getMenuDetails(id);
        return R.success(umsMenu);
    }

    /**
     * 修改后台菜单
     * @param id
     * @param umsMenu
     * @return
     */
    public R updateMenu(Long id, UmsMenu umsMenu) {
        umsMenuService.updateMenu(id,umsMenu);
        return R.success("修改成功");
    }

    /**
     * 修改菜单显示状态
     * @param id
     * @param hidden
     * @return
     */
    public R updateHidden(Long id, Integer hidden) {
        umsMenuService.updateHidden(id,hidden);
        return R.success("修改成功");
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     * @return
     */
    public R delete(Long id) {
        Boolean delete = umsMenuService.delete(id);
        if (delete) {
            return R.success("删除成功");
        } else {
            return R.error(101,"拥有子菜单,不能删除");
        }
    }
}
